package com.jianyun.jyzs.utils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.UserDao;
import com.jianyun.jyzs.fragment.MeFragment;

import org.json.JSONObject;

/**
 * Created by yangshijun on 2018/9/6.
 */

public class SetModuleFmHandler extends Handler {

    public static final int GETUSER = 99;
    public static final int UPDATEICON = 98;

    private MeFragment fm;

    private SetModuleFmHandler() {
        super();
    }

    // 带有Looper参数的构造函数
    private SetModuleFmHandler(Looper L) {
        super(L);
    }

    public SetModuleFmHandler(MeFragment fm) {
        this.fm = fm;
    }

    @Override
    public void handleMessage(Message msg) {
        try {
//            if(fm.dialog != null && fm.dialog.isShowing()){
//                fm.dialog.dismiss();
//            }
            HttpResultFmVO vo = null;
            if (msg.getData() != null) {
                Bundle bundle = msg.getData();
                vo = (HttpResultFmVO) bundle.getSerializable(SysConstant.msg_key_parcelable);
            }
            if (vo != null && !vo.isNormal) {
                Toast.makeText(fm.getActivity(), vo.err, Toast.LENGTH_SHORT).show();
                return;
            }
            int type = 0;
            String[] val = null;
            if (vo.param != null && vo.param instanceof Integer) {
                type = (Integer) vo.param;
            } else if (vo.param != null && vo.param instanceof String[]) {
                val = (String[]) vo.param;
                type = Integer.valueOf(val[0]);
            }
            switch (type) {
                /*case GETUSER:{
                    JSONObject object = new JSONObject(vo.json);
                    if(object.getBoolean("result")){//正常返回
                        User user = JSONObjectUtil.object2User(object);
                        if(user != null){
                            //user.setId(fm.userinfo.getId());
                            user.setPassword(fm.user.getPassword());
                            user.setLocalIconPath(fm.user.getLocalIconPath());
                            user.setUserType(fm.user.getUserType());
                            user.setLoginState(fm.user.getLoginState());
                            user.setUserId(fm.userId);
                            user.setEnterpriseCode(fm.enterpriseCode);
                            new UserDao(fm.getActivity()).update(user);
                            String iconPathId = "";
                            try {
                                iconPathId = object.getString("iconPathId");
                            } catch (Exception e) {
                            }
                            //保存文件
                            PreferrenceUtil.saveUserData(fm.getActivity(), user.getName(),
                                    user.getPassword(),
                                    user.getId(),
                                    user.getServerUserId(),
                                    user.getEnterpriseCode(),
                                    user.getNameZh(),
                                    iconPathId);
                            fm.user = user;
                            if(user.getLocalIconPath() != null && !user.getLocalIconPath().equals("")){
                                fm.imageLoader.displayImage(user.getLocalIconPath(), fm.head,fm.options);
                            }else if(user.getServerIconPath() != null && !user.getServerIconPath().equals("")){
                                fm.imageLoader.displayImage(PreferrenceUtil.getPreferencesData(fm.getActivity(), SysConstant.LOGIN_ERPROOTURL)+user.getServerIconPath(), fm.head,fm.options);
                            }
                            fm.contactUser.setLocalIconPath(fm.user.getLocalIconPath());
                            fm.contactUser.setServerIconPath(user.getServerIconPath());
                            new UserDao(fm.getActivity()).update(fm.contactUser);
                        }
                    }else{
                        Toast.makeText(fm.getActivity().getApplicationContext(), object.getString("message"),Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                case UPDATEICON:{
                    JSONObject object = new JSONObject(vo.json);
                    if(object.getBoolean("result")){//正常返回
                        fm.user.setLocalIconPath("file://"+val[1]);
                        fm.user.setServerIconPath(object.getString("iconPath"));
                        new UserDao(fm.getActivity()).update(fm.user);
                        fm.imageLoader.displayImage(fm.user.getLocalIconPath(), fm.head,fm.options);
                        fm.contactUser.setLocalIconPath("file://"+val[1]);
                        fm.contactUser.setServerIconPath(object.getString("iconPath"));
                        new UserDao(fm.getActivity()).update(fm.contactUser);
                        Toast.makeText(fm.getActivity().getApplicationContext(), fm.getActivity().getString(R.string.text_operation_msg),Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(fm.getActivity().getApplicationContext(), object.getString("message"),Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                default:
                    break;
            }*/
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
